﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_VBPQ_ContentEditor
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub setPermission()
        lbtAdd.Visible = HasPermission(Function_Name.VBPQ, Session("RoleID"), hidStatusId.Value, Audit_Type.Create)
        lbtView.Visible = HasPermission(Function_Name.VBPQ, Session("RoleID"), hidStatusId.Value, Audit_Type.ViewContent)
        lbtEdit.Visible = HasPermission(Function_Name.VBPQ, Session("RoleID"), hidStatusId.Value, Audit_Type.Edit)
        btnDelete.Visible = HasPermission(Function_Name.VBPQ, Session("RoleID"), hidStatusId.Value, Audit_Type.Delete)
    End Sub
    Sub LoadListAction(ByVal iContentId As Integer)
        Using Data As New VSEntities
            'Get Current StatusId of object
            Dim cst = (From q In Data.VBPQDocuments Where q.Id = iContentId Select q).SingleOrDefault
           hidStatusId.Value = cst.StatusId
            Dim iCurrentStatusId As Byte = cst.StatusId
            'Load action tuong ung voi current statusid
            Dim st = (From q In Data.StatusActions Where q.CurrentStatusId = iCurrentStatusId And q.FunctionId = Function_Name.VBPQ Select q Order By
                     q.ActionId, q.Action.Title, q.Action.AuditNumber).ToList()
            For Each i In st
                If HasPermission(Function_Name.VBPQ, Session("RoleID"), i.CurrentStatusId, i.Action.AuditNumber) Then
                    Dim _item As New ListItem("---" & i.Action.Title, i.ActionId)
                    cboAction.Items.Insert(0, _item)
                End If
            Next
            Dim _status = (From q In Data.Status Where q.StatusId = iCurrentStatusId And q.FunctionId = Function_Name.VBPQ).FirstOrDefault
            If Not _status Is Nothing Then
                Dim lstItem As New ListItem(_status.Title, "")
                cboAction.Items.Insert(0, lstItem)
                cboAction.SelectedIndex = -1
            End If
        End Using
    End Sub
    Protected Sub cboAction_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboAction.SelectedIndexChanged
        'Nếu trạng thái đang chọn khác với trạng thái ban đầu thì mới đi chuyển trạng thái
        If (cboAction.SelectedValue <> cboAction.Items(0).Value) Then


            Using data As New VSEntities
                Dim intId = hidID.Value
                Dim h = (From p In data.VBPQDocuments Where p.Id = intId Select p).FirstOrDefault
                Dim _StatusId = (From q In data.StatusActions Where q.CurrentStatusId = h.StatusId And q.ActionId = cboAction.SelectedValue _
                            And q.FunctionId = Function_Name.VBPQ _
                              Select q.NextStatusId).FirstOrDefault
                h.StatusID = _StatusId
                data.SaveChanges()
                Excute_Javascript("Alertbox('Thay đổi trạng thái thành công');", Me.Page, True)

            End Using

        End If
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            setPermission()
            If Not Request.QueryString("Id") Is Nothing Then
                hidID.Value = Request.QueryString("Id")
                'Load list action status
                LoadListAction(hidID.Value)
                setPermission()
                'btnDelete.Visible = HasPermission(Function_Name.CertifiedHousehold, Session("RoleID"), 0, Audit_Type.Delete)
                btnDelete.Attributes.Add("onclick", "return ComfirmDialog('Bạn có chắc chắn muốn xóa không?',0,'" + btnDelete.ClientID + "','" + hidID.Value.ToString + "',1);")
            End If


            'active tab tuong ung voi Page
            'InVisble button ko can thiet
            If Request.PhysicalPath.Contains("List.aspx") Then
                lbtList.CssClass = "current_manage"
                lbtList.Text = "<span class=""current_manage"">Quản lý</span>"
                lbtEdit.Visible = False
                lbtView.Visible = False
                btnDelete.Visible = False
                cboAction.Visible = False
                lblStatus.Visible = False
                'lbtAdd.Visible = HasPermission(Function_Name.Subject, Session("RoleID"), 0, Audit_Type.Create)

            ElseIf Request.PhysicalPath.Contains("Create.aspx") Then
                lbtAdd.CssClass = "current_manage"
                lbtAdd.Text = "<span class=""current_manage"">Thêm mới</span>"
                lbtEdit.Visible = False
                lbtView.Visible = False
                btnDelete.Visible = False
                cboAction.Visible = False
                lblStatus.Visible = False
                'Permission
                'lbtList.Visible = HasPermission(Function_Name.Subject, Session("RoleID"), 0, Audit_Type.Create)

            ElseIf Request.PhysicalPath.Contains("Detail.aspx") Then
                lbtView.CssClass = "current_manage"
                lbtView.Text = "<span class=""current_manage"">Xem</span>"
                'Permission


            ElseIf Request.PhysicalPath.Contains("Edit.aspx") Then
                lbtEdit.CssClass = "current_manage"
                lbtEdit.Text = "<span class=""current_manage"">Sửa</span>"
               
            End If
        End If
    End Sub
    Protected Sub lbtList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtList.Click
        Response.Redirect("List.aspx")
    End Sub
    Protected Sub lbtAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtAdd.Click
        Response.Redirect("Create.aspx")
    End Sub
    Protected Sub lbtEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtEdit.Click
        Response.Redirect("Edit.aspx?id=" & hidID.Value)
    End Sub
    Protected Sub lbtView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtView.Click
        Response.Redirect("Detail.aspx?id=" & hidID.Value)
    End Sub
    Protected Sub lnkbtnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        Dim intId As Integer
        Dim strLogName As String = ""
        Dim data As New VSEntities
        intId = hidID.Value
        Dim q = (From p In data.Subjects Where p.SubjectId = intId Select p).FirstOrDefault
        Try
            data.Subjects.DeleteObject(q)
            data.SaveChanges()
            Excute_Javascript("Alertbox('Xóa cơ quan ban hành thành công.');window.location ='../../Page/VBPQ/Promulgator.aspx';", Me.Page, True)
        Catch ex As Exception
            log4net.Config.XmlConfigurator.Configure()
            log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
            Excute_Javascript("Alertbox('Xóa cơ quan banh hành thất bại.');", Me.Page, True)
        End Try
    End Sub
#End Region
End Class
